#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   lsswendpt command
#
#   Syntax:  lsswendpt [ -F <format> | --help ]
#
#            --help          - Prints this message
#            -F <"format">   - a delimiter separated list of names
#                            representing desired properties to query.
#                            Valid values are:
#                                 frame
#                                 cage
#                                 server
#                                 up_total
#                                 adapter
#                                 network
#                                 plane
#                                 location
#                                 port
#                                 link_status 
#                                 route_status
#				  path_status 
#                     If -F is not specified, all properties are shown.
#
####################################################################
#
#   Initial version - 09/06/02
#
####################################################################

use Getopt::Long;

sub bynumber { $a <=> $b; }

sub exit_routine {
    
     # remove files from /tmp 
     
     exit $ret; 

} # end exit_routine subroutine

################################################################################
#
# subroutines to parse properties desired (-F parm)
#
################################################################################
sub parse_props {
	$parm_length = length($props_parm);
	if (substr($props_parm,0,1) eq ":") {
                        # There was a colon at the beginning
			#      of the -F value
#			print "Colon in the beginning\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

	if (substr($props_parm,$parm_length-1) ne ":") {
                        # There was not a colon at the end
			#      of the -F value
#			print "No colon at the end\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

#	($p1,$p2,$p3,$p4,$p5,$p6,$p7,$p8,$9) = split /:/, $props_parm, 9;

	# Find number of colons used
	$NUM_COLONS = 0;
	while ($props_parm =~ m/:/g) {
		$NUM_COLONS += 1;
	}

	@prop_list = split /:/, $props_parm, $MAX_PROPS + 1;

	# Now examine fields passed
        for ($i = 0; $i < $NUM_COLONS; $i++) {
		$prop = $prop_list[$i];
		# Were the fields specified properly?
		if ($prop eq "") {
#			print "There was a NULL field passed.\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
		}
		else {
			# Check the field name
			$FOUND = 0;
			for ($j = 0; $j < $MAX_PROPS; $j++) {
				if ($prop eq $DEF_PROPS[$j]) {
					if ($FOUND_PROPS[$j] == 1) {
						# Second time for this prop
#						print "SECOND time\n"; #DEBUG
						print $USAGE;
						$ret = 1;
						&exit_routine;
					}
					else {
						$FOUND = 1;
						$FOUND_PROPS[$j] = 1;
						$ORDER[$NUM_PROPS] = $j;
					}
				}
			}

			# Was the field name a valid field name?
			if ($FOUND == 0) {
#				print "Field name NOT found\n";     #DEBUG
				print $USAGE;
				$ret = 1;
				&exit_routine;
			}

			$NUM_PROPS += 1;
			
#			# Were an incorrect number of fields passed?
#		        if ($NUM_PROPS > $MAX_PROPS) {
#				print "Too MANY props passed\n";        #DEBUG
#				print $USAGE;
#			}
		}
	}
}


################################################################################
#
# credentials subroutines
#
################################################################################
sub remove_creds {
        system "/bin/rm $RCMDTKFILE > /dev/null 2>&1";
        if ($DCE_tickets == 1) {
                `/bin/kdestroy`;
                 delete $ENV{'KRB5CCNAME'};
        }
} # end remove_creds subroutine

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "lsswendpt";
$BIN    = "/opt/hsc/bin"; 
$PARMS = 0;

$NUM_PROPS = 0;
$MAX_PROPS = 12;
@DEF_PROPS = ("frame" , "cage" , "server" , "up_total" , "adapter" , "network" , "plane", "location", "port", "link_status", "route_status", "path_status" );
@FOUND_PROPS = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
@HEAD = (" frame ", " cage ", "          server MTMS ", " up/total ", " adapter ", " network ", " plane ", "  location code ", " port ", " link_status ", " route_status ", " path_status ");
@FORMAT = (" %5s ", " %4s ", " %20s ", " %8s ", " %7s ", " %7s ", " %5s ", " %14s ", " %4s ", " %s ", " %15s ", " %10s ");

$USAGE = "Usage:  lsswendpt [ -F <format> | --help ]\n";

# First check for existence of parameters
if ($#ARGV ne -1) {
	$PARMS = 1;
}


# If there are parms, need to validate and parse
if ($PARMS == 1) {

	Getopt::Long::Configure("no_ignore_case");
	$result = GetOptions(
	        "help" => \$help,
	        "F=s" => \$Format,
	);

#print "GetOptions rc is ", $result, " .\n";

	if ($result == 0) {
		# Bad result from GetOptions
#		print "Bad result from GetOptions\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

	if ($help) {
		# help was asked for
#		print "help was specified\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 0;
		&exit_routine;
	}

	$props_parm = $Format;  # colon-separated list of column names

	if ($props_parm ne "") {
		&parse_props;
	}
	else {
		# Nothing else was specified after -F
#		print "Nothing after -F -- shouldn't get here\n";     #DEBUG
		print $USAGE;
		$ret = 1;
		&exit_routine;
	}
}
else {
	# -F was not specified, so use default
	$NUM_PROPS = $MAX_PROPS;
        @FOUND_PROPS = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
        @ORDER = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
}

# Now that we know what the issuer wants to see, let's get all the data
chop(@DATA = `CLASSPATH=/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo epview`);
#chop(@DATA = `cd /opt/hsc/com/ibm/hsc/auiml/databeans/snm; java com.ibm.hsc.auiml.databeans.snm.SnmInfo epview`);

if ($DATA[0] eq 0) {
#	print "SnmInfo returned 0.\n";             #DEBUG
	print "There is no data to display.\n";
	$ret = 1;
	&exit_routine;
}

$lines = 0;
foreach (@DATA) {
	($type, $f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8, $f9) = split /\s+/;
	if ($type eq "BOARD_LINE") {
		$OUTPUT[$lines]{0} = "BOARD_LINE";
		$OUTPUT[$lines]{1} = $f1;   # frame
		$OUTPUT[$lines]{2} = $f2;   # cage
		$OUTPUT[$lines]{3} = $f3;   # server MTMS
		$OUTPUT[$lines]{4} = $f4;   # adapters up/total
		$OUTPUT[$lines]{5} = " ";   # adapter
		$OUTPUT[$lines]{6} = " ";   # network
		$OUTPUT[$lines]{7} = " ";   # plane
		$OUTPUT[$lines]{8} = " ";   # location
		$OUTPUT[$lines]{9} = " ";   # port
		$OUTPUT[$lines]{10} = $f5;   # status
		$OUTPUT[$lines]{11} = $f6;   # route table status
		$OUTPUT[$lines]{12} = "     $f7";   # path table  status 
	}
	elsif ($type eq "DEV_LINE") {
		$OUTPUT[$lines]{0} = "DEV_LINE";
		$OUTPUT[$lines]{1} = " ";   # frame
		$OUTPUT[$lines]{2} = " ";   # cage
		$OUTPUT[$lines]{3} = " ";   # server MTMS
		$OUTPUT[$lines]{4} = " ";   # adapters up/total
		$OUTPUT[$lines]{5} = $f1;   # adapter
		$OUTPUT[$lines]{6} = $f2;   # network
		$OUTPUT[$lines]{7} = $f3;   # plane
		$OUTPUT[$lines]{8} = $f5;   # location
		$OUTPUT[$lines]{9} = " ";   # port
		$OUTPUT[$lines]{10} = $f4;   # status
		$OUTPUT[$lines]{11} = $f6. " ".$f7;   # route table status
		$OUTPUT[$lines]{12} = $f8." ".$f9;   # path table status
	}
	elsif ($type eq "PORT_LINE") {
		$OUTPUT[$lines]{0} = "PORT_LINE";
		$OUTPUT[$lines]{1} = " ";   # frame
		$OUTPUT[$lines]{2} = " ";   # cage
		$OUTPUT[$lines]{3} = " ";   # server MTMS
		$OUTPUT[$lines]{4} = " ";   # adapters up/total
		$OUTPUT[$lines]{5} = " ";   # adapter
		$OUTPUT[$lines]{6} = " ";   # network
		$OUTPUT[$lines]{7} = " ";   # plane
		$OUTPUT[$lines]{8} = " ";   # location
		$OUTPUT[$lines]{9} = $f1;   # port
		$OUTPUT[$lines]{10} = $f2." ".$f3." ".$f4." ".$f5;   # status
		$OUTPUT[$lines]{11} = " ";   # route table status
                $OUTPUT[$lines]{12} = " ";   # path table status
	}
	$lines += 1;
}

# If the user did not specify any parms, print out a header line
if ($PARMS == 0) {
	foreach (@ORDER) {
		$HEADER = $HEADER.$HEAD[$_];
	}
	print $HEADER."\n";
}

# Now for each line of data, we'll set up an output line with only the
#      data columns the user asked for
for ($k = 0; $k < $lines; $k++) {
	$LINE_OUT = "";      # Blank out output line

	# First see if we care about the data line - if it's a BOARD_LINE,
	#      we only care if one of the first four columns were asked for
	if (($OUTPUT[$k]{0} eq "BOARD_LINE") && ($FOUND_PROPS[0] || $FOUND_PROPS[1] || $FOUND_PROPS[2] || $FOUND_PROPS[3] || $FOUND_PROPS[9])) {
		# Now according to the order of the fields requested,
		#     we'll generate the output line
		foreach (@ORDER) {
			# If no parms were passed, we'll generate the output
			#     line with each field's format and data for that
			#     field;  otherwise, we'll just output the data
			#     separated by colons
			if ($PARMS == 0) {
				$FIELD_OUT = sprintf "$FORMAT[$_]", $OUTPUT[$k]{$_+1};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			else {
				# Is there a value to print?
				if ($OUTPUT[$k]{$_+1} ne " ") {
					$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_+1}.":";
				}
				else {
					# If not, just add a colon
					$LINE_OUT = $LINE_OUT.":";
				}
			}
		}
		print $LINE_OUT."\n";
	}
	# If it's a DEV_LINE, we only care if a DEV_LINE column 
	#      or if the last column was asked for
	elsif (($OUTPUT[$k]{0} eq "DEV_LINE") && ($FOUND_PROPS[4] || $FOUND_PROPS[5] || $FOUND_PROPS[6] || $FOUND_PROPS[7] || $FOUND_PROPS[9] || $FOUND_PROPS[10] || $FOUND_PROPS[11])) {
		# Now according to the order of the fields requested,
		#     we'll generate the output line
		foreach (@ORDER) {
			# If no parms were passed, we'll generate the output
			#     line with each field's format and data for that
			#     field;  otherwise, we'll just output the data
			#     separated by colons
			if ($PARMS == 0) {
				$FIELD_OUT = sprintf "$FORMAT[$_]", $OUTPUT[$k]{$_+1};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			else {
				# Is there a value to print?
				if ($OUTPUT[$k]{$_+1} ne " ") {
					$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_+1}.":";
				}
                                else {
                                        # If not, just add a colon
                                        $LINE_OUT = $LINE_OUT.":";
                                }
			}
		}
		print $LINE_OUT."\n";
	}
	# If it's a PORT_LINE, we only care if a PORT_LINE column 
	#      or if the last column was asked for
	elsif (($OUTPUT[$k]{0} eq "PORT_LINE") && ($FOUND_PROPS[8] || $FOUND_PROPS[9])) {
		# Now according to the order of the fields requested,
		#     we'll generate the output line
		foreach (@ORDER) {
			# If no parms were passed, we'll generate the output
			#     line with each field's format and data for that
			#     field;  otherwise, we'll just output the data
			#     separated by colons
			if ($PARMS == 0) {
				$FIELD_OUT = sprintf "$FORMAT[$_]", $OUTPUT[$k]{$_+1};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			else {
				# Is there a value to print?
				if ($OUTPUT[$k]{$_+1} ne " ") {
					if ($DEF_PROPS[$_] eq "link_status") {
						# If this is for status, we
						#   need to use quotes
						$LINE_OUT = $LINE_OUT."\"".$OUTPUT[$k]{$_+1}."\":";
					}
					else {
						$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_+1}.":";
					}
				}
                                else {
                                        # If not, just add a colon
                                        $LINE_OUT = $LINE_OUT.":";
                                }
			}
		}
		print $LINE_OUT."\n";
	}
}

$ret = 0;
$exit_routine;

